package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.UserMapper;
import com.example.finalsystem.Entity.Deal;
import com.example.finalsystem.Entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class    MemberService {
    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = MemberService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }
    public List<Deal> getDealsByUser(String userId)
    {
        SqlSession sqlSession = openSession();

        User user =   sqlSession.getMapper(UserMapper.class).getUserByDeal(userId);
        sqlSession.close();
     return user.getDeals();
    }


    public User getPerson(String userId)
    {
        SqlSession sqlSession = openSession();

        User u =sqlSession.getMapper(UserMapper.class).selectByPrimaryKey(userId);
        sqlSession.close();
        return u;
    }

    public String addPerson(String userId,String name,String password){
        User u = new User();
        u.setUserid(userId);
        u.setUsername(name);
        u.setUserpassword(password);
        u.setUserrole("STAFF");
        SqlSession sqlSession = openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        if (userMapper.selectByPrimaryKey(userId)!=null)
        {
            return "该账户已存在";
        }
        userMapper.insert(u);
        sqlSession.commit();
        sqlSession.close();
        return "添加成功";
    }
//    账号不允许修改
    public String fixPerson(String userId,String name ,String password,String role){
        SqlSession sqlSession = openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User u = userMapper.selectByPrimaryKey(userId);
        if (u==null)
        {
            return "该账户不存在";
        }
        u.setUserpassword(password);
        u.setUsername(name);
        u.setUserrole(role);
        userMapper.updateByPrimaryKey(u);
        sqlSession.commit();
        sqlSession.close();
        return "修改成功";
    }
    public String deletePerson(String userId){
        SqlSession sqlSession = openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User u = userMapper.selectByPrimaryKey(userId);
        if (u==null)
        {
            return "该账户不存在";
        }
        userMapper.deleteByPrimaryKey(userId);
        sqlSession.commit();
        sqlSession.close();
        return "删除成功";
    }
   public List<User> getListOfUser(int currentPage , int pageSize,SqlSession sqlSession)
   {
       UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
       int index = (currentPage-1)*pageSize;
        return userMapper.getListOfUser(index,pageSize);
   }

   public int countMember()
   {
       SqlSession sqlSession = openSession();
       UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
       int i = userMapper.countMember();
       sqlSession.close();
       return i;
   }
}
